package cn.bigcontainer.alt.sys.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.bigcontainer.alt.common.util.WoJsonKit;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.dto.MAppDto;
import cn.bigcontainer.alt.meta.mapper.MAppMapper;
import cn.bigcontainer.alt.meta.po.MApp;
import cn.bigcontainer.alt.sys.exception.SysException;
import cn.bigcontainer.alt.sys.mapper.MenuMapper;
import cn.bigcontainer.alt.sys.po.Menu;

public class MenuDto extends Menu {

	public MenuDto () {
		super();
	}

	public MenuDto (Menu m) {
		WoKit.cloneBySetter(m, this);
		MenuMapper menuMapper = WoKit.getBean(MenuMapper.class);
		// 获取父菜单信息
		if (parentId != null) {
			Menu parent = menuMapper.selectById(parentId);
			this.parentName = parent.getName();
		}
		// 获取子菜单信息
		QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_id", this.id);
		this.children = WoKit.getDtos(MenuDto.class, menuMapper.selectList(queryWrapper));
		MenuDto.setAppData(this);
	}

	public static void setAppData(MenuDto m) {
		String appId = m.getAppId();
		if (!WoKit.isEmpty(appId)) {
			MApp app1 = WoKit.getApplicationContext().getBean(MAppMapper.class).selectById(appId);
			if (app1 == null) {
				throw new SysException(SysException.ERR_MENU_APP, appId);
			}
			MAppDto a = new MAppDto(app1);
			a.setDomprefix(m.getNo());
			a.getObject().getRequest().bigPutAll(m.getParameters());
			m.setApp(a);
		}
	}

	public static final String TYPE_MENU = "1";
	
	public static final String TYPE_NOTIFICATION = "2";
	
	public static final String TYPE_BUTTON = "3";
	
	/**
	 * 上级菜单名称
	 */
	private String parentName;

	/**
	 * 应用url
	 */
	private MAppDto app;

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	@JsonIgnore
	public MAppDto getApp() {
		return app;
	}

	public void setApp(MAppDto app) {
		this.app = app;
	}

	public List<MenuDto> getChildren() {
		return children;
	}

	public void setChildren(List<MenuDto> children) {
		this.children = children;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	/**
	 * 子菜单
	 */
	private List<MenuDto> children = new ArrayList<MenuDto>();

	/**
	 * 标识菜单是否是选中状态
	 */
	private Boolean active = false;

	public String getIcon2 () {
		if (WoKit.isEmpty(icon)) {
			if (this.parentId == null) {
				return "fa fa-folder";
			} else {
				return "fa fa-circle-o";
			}
		}
		return icon;
	}

	public String getActiveCls() {
		return active ? "active" : "";
	}

	/**
	 * 获取菜单url，并将request数据设置到App中
	 * @param req
	 * @return
	 */
	public String getUrl2(Map<String, Object> req) {
		if (app == null || WoKit.isEmpty(app.getTemplate())) {
			return url;
		}
		app.getObject().getRequest().bigPutAll(req);
		return app.getTemplate();
	}
	
	@JsonIgnore
	public String getParametersJson () {
		return WoJsonKit.writeAsString(this.parameters);
	}
}
